﻿using Server.Constant;
using Server.Enum;
using Server.Framework.Message;
using Server.Framework.Processor;
using Server.Framework.Service;
using Server.Framework.Socket;
using Server.Message.Request;
using Server.Message.Response;
using Server.Model;
using Server.OptExp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Server.Processor
{
    /// <summary>
    /// Author: Leil
    /// Description: 顶级菜单列表请求处理
    /// CreateTime: 2020/5/31 1:09:15
    /// </summary>
    [ProcessorAttr(MessageId.REQUEST_LOGIN_LOG)]
    class RequestLoginLogProcessor : IProcessor
    {
        public void Process(ProtobufAppSession session, RequestBaseMessage message)
        {
            if (session.User == null)
            {
                Console.WriteLine("当前用户未登录");
                return;
            }
            if (message is LoginLogReq msg)
            {
                string sql = $@"SELECT
	* 
FROM
	( SELECT * FROM SYS_LOGIN_LOG WHERE USER_ID = {session.User.ID} ORDER BY LOGIN_TIME DESC ) 
WHERE
	ROWNUM <=3";
                try
                {
                    LoginLogResp resp = new LoginLogResp(MessageId.RESPONSE_LOGIN_LOG);
                    DBService.Instance().ExecuteQuery(sql, reader =>
                    {
                        while (reader.Read())
                        {
                            LoginLog log = new LoginLog
                            {
                                Id = Convert.ToInt32(reader["ID"]),
                                LoginTime = reader["LOGIN_TIME"].ToString(),
                                LogoutTime = reader["LOGOUT_TIME"].ToString(),
                                LoginIp = reader["LOGIN_IP"].ToString()
                            };
                            resp.Logs.Add(log);
                        }
                    });
                    session.Send(resp);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }



    }
}
